package com.takeshequ;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.takeshequ.homepage.Member;
import com.takeshequ.main.MainFragmentActivity;
import com.takeshequ.regiest.RegisterActivity;
import com.utils.HttpClientUtils;
import com.utils.HttpClientUtils.HttpResponseHandler;

public class LoadingActivity extends BasicActivity {

	private View bottomView, registerView, loginView;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loading);

	};

	private boolean ifRegister() {
		System.out.println(sharedPreferencesUtils.getMemberId());
		if (StringUtils.isEmpty(sharedPreferencesUtils.getMemberId())) {
			return false;
		}
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		bottomView = findViewById(R.id.loading_bottom);
		registerView = findViewById(R.id.loading_register);
		loginView = findViewById(R.id.loading_login);
		if (ifRegister()) {
			bottomView.setVisibility(View.GONE);
			login(sharedPreferencesUtils.getPhone(),
					sharedPreferencesUtils.getPwd());
		} else {
			bottomView.setVisibility(View.VISIBLE);
			registerView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					startActivity(new Intent(LoadingActivity.this,
							RegisterActivity.class));
				}
			});
			loginView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					startActivity(new Intent(LoadingActivity.this,
							LoginActivity.class));
				}
			});
		}
	}

	private void getPersonInfo() {

		HttpClientUtils.getPersonalInfo(new HttpResponseHandler() {

			@Override
			public void handler(String response) {
				try {
					JSONObject jsonObject = new JSONObject(response);
					Constants.member = gson.fromJson(
							jsonObject.getString("member"), Member.class);
					Intent intent = new Intent(LoadingActivity.this,
							MainFragmentActivity.class);
					startActivity(intent);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

	}

	private void login(final String phoneno, final String pwd) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("nickName", sharedPreferencesUtils.getNickName());
		params.put("loginName", phoneno);
		params.put("password", pwd);
		HttpClientUtils.post(LoadingActivity.this, "正在连接,请稍后", "login", params,
				new HttpResponseHandler() {

					@Override
					public void handler(String response) {
						ResponseBody responseBody = gson.fromJson(response,
								ResponseBody.class);
						if (responseBody.getState().equals("1")) {
							sharedPreferencesUtils.setMemberId(responseBody
									.getMember().getMemberId());
							sharedPreferencesUtils.setPhone(phoneno);
							sharedPreferencesUtils.setPwd(pwd);
							sharedPreferencesUtils.setNickName(responseBody
									.getMember().getNickName());
							Constants.memberId = responseBody.getMember()
									.getMemberId();
							Constants.selectMemberId = Constants.memberId;
							JPushInterface.setAliasAndTags(
									LoadingActivity.this, responseBody
											.getMember().getMemberId(), null);
							getPersonInfo();
						} else {
							Toast.makeText(LoadingActivity.this,
									responseBody.getDesc(), 2000).show();
						}
					}
				});

	}
}
